package com.example.tianling.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tianling.dao.BookMapper;
import com.example.tianling.entity.BookEntity;
import com.example.tianling.service.BookService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class BookServiceImpl
        extends ServiceImpl<BookMapper, BookEntity>
        implements BookService {
    @Override
    public BookEntity getBookByBookId(Integer bookId) {
        BookEntity book = this.lambdaQuery()
                .eq(BookEntity::getBookId, bookId)
                .last("LIMIT 1")
                .one();
        return book;
    }

    @Override
    public List<BookEntity> getBooksByArea(String area, String type) {
        List<BookEntity> bookList = this.lambdaQuery()
                .select(BookEntity::getBookId, BookEntity::getBookName, BookEntity::getYear, BookEntity::getArea)
                .like(BookEntity::getArea, area)
                .like(BookEntity::getType, type)
                .list();
        return bookList;
    }

    @Override
    public BookEntity getDefaultBook(String type, String area) {
        List<BookEntity> bookList = this.lambdaQuery()
//                .eq(BookEntity::getArea, area)
                .eq(BookEntity::getType, type)
                .list();
        return bookList.get(0);
    }
    public BookEntity getNextBook(String type, String area,Integer bookId,String description,String keyword){
        BookEntity bookEntity = this.lambdaQuery()
                .select(BookEntity::getBookId, BookEntity::getBookName, BookEntity::getYear, BookEntity::getArea,BookEntity::getContent,BookEntity::getAnswer,BookEntity::getIsNeedVip)
                .like(BookEntity::getArea, area)
                .like(BookEntity::getType, type)
                .like(BookEntity::getDescription,description)
                .like(BookEntity::getBookName,keyword)
                .orderByAsc(BookEntity::getBookId)
                .gt(BookEntity::getBookId, bookId)  // 添加 book_id > 11 的条件
                .last("LIMIT 1")
                .one();
        return bookEntity;
    }

    @Override
    public BookEntity getLastBook(String type, String area, Integer bookId,String description,String keyword) {
        BookEntity bookEntity = this.lambdaQuery()
                .select(BookEntity::getBookId, BookEntity::getBookName, BookEntity::getYear, BookEntity::getArea,BookEntity::getContent,BookEntity::getAnswer,BookEntity::getIsNeedVip)
                .like(BookEntity::getArea, area)
                .like(BookEntity::getType, type)
                .like(BookEntity::getDescription,description)
                .like(BookEntity::getBookName,keyword)
                .orderByDesc(BookEntity::getBookId)
                .lt(BookEntity::getBookId, bookId)  // 添加 book_id < 11 的条件
                .last("LIMIT 1")
                .one();
        return bookEntity;
    }


    @Override
    public List<BookEntity> getBooksByAreaAndTheme(String area, String type,String describe,Integer pageNum,String keyword) {
        // 创建一个 Page 对象，指定当前页和每页记录数
        Page<BookEntity> page = new Page<>(pageNum, 10); // current 表示当前页码，size 表示每页记录数

        // 执行带分页的查询
        IPage<BookEntity> pageResult = this.lambdaQuery()
                .select(BookEntity::getBookId, BookEntity::getBookName, BookEntity::getYear, BookEntity::getArea,BookEntity::getContent,BookEntity::getIsNeedVip)
                .like(BookEntity::getArea, area)
                .like(BookEntity::getType, type)
                .like(BookEntity::getDescription, describe)
                .like(BookEntity::getBookName,keyword)
                .page(page);
        for(BookEntity bookEntity :pageResult.getRecords()){
            bookEntity.setContent(bookEntity.getContent().substring(0,20));
        }
        return pageResult.getRecords();
    }

    @Override
    public List<BookEntity> searchBooks(String keyword,Integer pageNum) {
        // 创建一个 Page 对象，指定当前页和每页记录数
        Page<BookEntity> page = new Page<>(pageNum, 10); // current 表示当前页码，size 表示每页记录数

        // 执行带分页的查询
        IPage<BookEntity> pageResult = this.lambdaQuery()
                .select(BookEntity::getBookId, BookEntity::getBookName, BookEntity::getYear, BookEntity::getArea,BookEntity::getContent,BookEntity::getIsNeedVip)
                .like(BookEntity::getBookName,keyword)
                .page(page);
        for(BookEntity bookEntity :pageResult.getRecords()){
            bookEntity.setContent(bookEntity.getContent().substring(0,20));
        }
        return pageResult.getRecords();
    }
    @Override
    public List<String> getIdea(Integer bookId){
        BookEntity bookEntity = this.lambdaQuery()
                .select(BookEntity::getIdeaOne, BookEntity::getIdeaTwo, BookEntity::getIdeaThree)
                .eq(BookEntity::getBookId,bookId)
                .last("LIMIT 1")
                .one();
        List<String> ideaList = new ArrayList<>();
        try {
            ideaList.add(bookEntity.getIdeaOne());
            ideaList.add(bookEntity.getIdeaTwo());
            ideaList.add(bookEntity.getIdeaThree());
        } catch (NullPointerException e) {
            ideaList.add(" ");
        }
        return ideaList;
    }
 @Override
    public String getRightAnnswer(Integer bookId){
        BookEntity bookEntity = this.lambdaQuery()
                .select(BookEntity::getAnswer)
                .eq(BookEntity::getBookId,bookId)
                .one();

        String result = "";
        if(bookEntity!=null){
            result = bookEntity.getAnswer();
        }
        return result;
    }

    @Override
    public void orderBook() {
        List<BookEntity> allList = null;
        allList = new ArrayList<>();

        List<BookEntity> jiegouhua_area = this.lambdaQuery()
                .select(BookEntity::getArea)
                .eq(BookEntity::getType, "结构化")
                .list();
        Set<BookEntity> jiegouhua_area_set = new HashSet<>(jiegouhua_area);

        List<BookEntity> wulingdao_area = this.lambdaQuery()
                .select(BookEntity::getArea)
                .eq(BookEntity::getType, "无领导")
                .list();
        Set<BookEntity> wulingdao_area_set = new HashSet<>(wulingdao_area);

        for (BookEntity book : jiegouhua_area_set) {
            List<BookEntity> jiegouhua_list = this.lambdaQuery()
                    .select(BookEntity::getArea, BookEntity::getBookName, BookEntity::getSummuny, BookEntity::getAnswer,
                            BookEntity::getDescription, BookEntity::getType, BookEntity::getIsNeedVip,BookEntity::getYear,
                            BookEntity::getContent, BookEntity::getIdeaOne, BookEntity::getIdeaTwo, BookEntity::getIdeaThree)
                    .eq(BookEntity::getType, "结构化")
                    .eq(BookEntity::getArea, book.getArea())
                    .orderByDesc(BookEntity::getYear)
                    .list();
            allList.addAll(jiegouhua_list);
        }
        for (BookEntity book : wulingdao_area_set) {
            List<BookEntity> wulingdao_list = this.lambdaQuery()
                    .select(BookEntity::getArea, BookEntity::getBookName, BookEntity::getSummuny, BookEntity::getAnswer,
                            BookEntity::getDescription, BookEntity::getType, BookEntity::getIsNeedVip,BookEntity::getYear,
                            BookEntity::getContent, BookEntity::getIdeaOne, BookEntity::getIdeaTwo, BookEntity::getIdeaThree)
                    .eq(BookEntity::getType, "无领导")
                    .eq(BookEntity::getArea, book.getArea())
                    .orderByDesc(BookEntity::getYear)
                    .list();
            allList.addAll(wulingdao_list);
        }

        baseMapper.deleteAllBook();

        this.saveBatch(allList);
    }
}
